package com.kingja.sample.callback;

import com.kingja.loadsir.callback.Callback;
import com.kingja.sample.ResourceTable;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.components.RoundProgressBar;
import ohos.app.Context;

public class AnimateCallback extends Callback {
    @Override
    protected int onCreateView() {
        return ResourceTable.Layout_layout_animate;
    }

    private AnimatorValue animator;
    @Override
    public void onAttach(Context context, Component view) {
        super.onAttach(context, view);

        RoundProgressBar bar= (RoundProgressBar) view.findComponentById(ResourceTable.Id_loading_bar);
        if(bar==null){
            return;
        }

        animator = new AnimatorValue();
        animator.setDuration(1500);
        animator.setLoopedCount(-1);
        animator.setCurveType(Animator.CurveType.ACCELERATE);

        animator.setValueUpdateListener((animatorValue, value) -> {
            bar.setRotation((int) (360 * value));
        });
        animator.start();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if(animator!=null){
            animator.stop();
            animator=null;
        }
    }
}
